package com.tihi.uiautomator;

import android.app.Service;
import android.content.Intent;
import android.os.Handler;
import android.os.IBinder;

import com.tihi.floatview.FloatViewMgr;
import com.tihi.floatview.FloatViewMgr.IFloatViewLis;
import com.tihi.handsuiviewer.App;
import com.tihi.handsuiviewer.UIAnaActivity;
import com.tihi.uiautomator.eventmsg.FloatViewDismissMsg;
import com.tihi.uiautomator.eventmsg.UpdateFloatViewMsg;
import com.tihi.util.AppUtils;

/**
 * 悬浮窗后台service控制
 * @author duantihi
 *
 * @date 2016-2-1 下午3:44:22
 */
public class FloatViewService extends Service{
	private FloatViewMgr floatViewMgr;
	
	public static void startService() {
		Intent intent = new Intent(App.instance(), FloatViewService.class);
		App.instance().startService(intent);
	}
	
	public static void stopService() {
		Intent intent = new Intent(App.instance(), FloatViewService.class);
		App.instance().stopService(intent);
	}
	
	@Override
	public IBinder onBind(Intent intent) {
		return null;
	}
	
	@Override
	public void onCreate() {
		super.onCreate();
		App.getAppEventBus().register(this);
		floatViewMgr = FloatViewMgr.instance();
//		floatViewMgr.show();
		floatViewMgr.setListener(new IFloatViewLis() {
			
			@Override
			public void onClick() {
				//打开新activity，必须在该service启动，且必须设置Intent.FLAG_ACTIVITY_NEW_TASK
				startUIDump();
				floatViewMgr.setClickEnable(false);
			}
		});
	}
	
	@Override
	public int onStartCommand(Intent intent, int flags, int startId) {
		return super.onStartCommand(intent, flags, startId);
	}
	
	@Override
	public void onDestroy() {
		super.onDestroy();
		App.getAppEventBus().unregister(this);
		floatViewMgr.dismiss();
	}
	
	/**
	 * 收到 MainActivity发出的 UpdateFloatViewMsg
	 * @param msg
	 */
	public void onEventMainThread(UpdateFloatViewMsg msg) {
		if(floatViewMgr.isShowing()) {
			floatViewMgr.dismiss();
		}else {
			floatViewMgr.show();
		}
		floatViewMgr.setStatus("开始");
	}
	
	/**
	 * 开始dump ui xml
	 * @param msg
	 */
	public void startUIDump() {
		floatViewMgr.setStatus("dumping ui xml…");
		new UIAutomator(getUIAutomatorListener()).submit();
	}
	
	/**
	 * uiautomator 回调
	 * @return
	 */
	private IShellCmdListener getUIAutomatorListener() {
		return new IShellCmdListener() {
			
			@Override
			public void onResult(boolean success) {
				if(success) {
					floatViewMgr.setStatus("screenCapping…");
					floatViewMgr.dismiss();
					App.getAppEventBus().post(new FloatViewDismissMsg());
					new Handler().postDelayed(new Runnable() {
						
						@Override
						public void run() {
							new ScreenCap(getScreenCapListener()).cap();
						}
					}, 450);
				}else {
					floatViewMgr.setStatus("dump ui failed");
					floatViewMgr.setClickEnable(true);
					AppUtils.toast("ui dump失败，请root手机！");
					new Handler().postDelayed(new Runnable() {
						
						@Override
						public void run() {
							floatViewMgr.setStatus("开始");
						}
					}, 1000);
				}
			}
		};
	}
	
	private IShellCmdListener getScreenCapListener() {
		return new IShellCmdListener() {
			
			@Override
			public void onResult(boolean success) {
				floatViewMgr.setClickEnable(true);
				if(success) {
					//显示图片，开始xml解析
					UIAnaActivity.startAnaActivity(FloatViewService.this);
				}else {
				}
			}
		};
	}

}
